//
//  FeedBackView.swift
//  ptbuy
//
//  Created by MyMac on 2024/12/2.
//

import SwiftUI
/*
 意见反馈
 */
struct FeedBackView: View {
    @Binding var feedback:Bool
    
    @State private var text = ""
    @State private var submit:Bool = false
    @State private var submitSuccessStatus:Bool = false
    @State private var submitfailStatus:Bool = false
    @State private var submitfailTextIsEmpty:Bool = false
    @FocusState private var isFocused: Bool//控制鼠标点击外部关闭软键盘
    
    var body: some View {
        NavigationView {
            VStack{
                Form {
                    Section(header: Text("")) {
                        VStack{
                            HStack{
                                Text("请输入您的建议:")
                                    .font(.headline)
                                Spacer()
                            }
                           
                            TextEditor(text: $text)
                                .padding()
                                .border(Color.gray, width: 1)
                                .frame(height: 200)
                                .onTapGesture {
                                    // 点击事件的代码
                                    isFocused = true
                                }.focused($isFocused)
                                   
                        }.padding([.top,.bottom],8)
                                
                    }
                            
                }
                Button(action:{
                    if text.isEmpty{
                        submitfailTextIsEmpty = true
                    }else{
                        submit = true
                        submitFeed()
                    }
                   
                }) {
                    Text("提交").padding().frame(
                        maxWidth: .infinity,
                        alignment: .center
                    )
                }.alert("提交成功", isPresented:$submitSuccessStatus){
                    
                    Button("关闭",role:.cancel){
                        submitSuccessStatus = false
                    }
                }.alert("提交失败", isPresented:$submitfailStatus){
                    
                    Button("关闭",role:.cancel){
                        submitfailStatus = false
                    }
                }.alert("请填写您的建议", isPresented:$submitfailTextIsEmpty){
                    
                    Button("关闭",role:.cancel){
                        submitfailTextIsEmpty = false
                    }
                }
                .overlay(
                    RoundedRectangle(
                        cornerRadius: 8
                    )
                    .stroke(
                        Color.black,
                        lineWidth: 0.5
                    ) // 设置边框
                ).edgesIgnoringSafeArea(
                    .all
                ) // 忽略安全区域，使视图覆盖整个屏幕
                .cornerRadius(
                    8
                ) // 设置最大宽高为无穷，确保视图覆盖全屏
                Spacer()
            }.overlay{
                if submit{
                    Group{
                        ProgressView("正在提交...")
                            .progressViewStyle(CircularProgressViewStyle())
                            .padding()
                            .background(Color.white)
                            .cornerRadius(10)
                            .shadow(radius: 10)
                            .frame(maxWidth: .infinity,minHeight:200)
                    }
                }
               
            }.onTapGesture {
                isFocused = false
            } .navigationBarTitle("意见反馈", displayMode: .inline) // 设置导航栏标题
                .navigationViewStyle(StackNavigationViewStyle()) // 推荐在全屏时使用此样式
                .toolbar {
                    // 修改标题字体大小
                    ToolbarItem(placement: .topBarLeading) {
                        Text("返回")
                            .clipShape(
                                Capsule()
                            ) // 使用 Capsule 形状来实现圆角效果
                            .onTapGesture {
                                feedback = false
                            }
                    }
                }
           
        }.background(Color.white).edgesIgnoringSafeArea(.all)
    }
    
    func submitFeed(){
        //参数
        let parameters = [
            "content": text
        ]
        
        NetworkApi.shared
            .fetchData(
                from: "feedback/addFeedback",
                parameters:parameters
            ){data,error in
            
                submit = false
                text = ""
                if let error = error{
                    submitfailStatus = true
                    return
                }
            
            
                if let data = data {
                    // 尝试将 Data 转换为字符串
                    if let string = String(data: data, encoding: .utf8) {
                        if string == "success"{
                            submitSuccessStatus = true
                        }else{
                            submitfailStatus = true
                        }
                    } else {
                        print("无法将数据转换为字符串")
                        submitfailStatus = true
                    }
                }
            }
    }
}
